// client main.c
#include <my_header.h>
#include "transFile.h"

//int recvFile(int netfd){
//    train_t train;
//    char filename[1024] = { 0 };
//    // 收文件名
//    recv(netfd, &train._length, sizeof(int), MSG_WAITALL);
//    recv(netfd, train._data, train._length, MSG_WAITALL);
//    memcpy(filename, train._data, train._length);
//    printf("filename = %s\n", filename);
//    
//    int filefd = open(filename, O_RDWR | O_CREAT | O_TRUNC, 0666);// 打开文件注意默认权限
//    ERROR_CHECK(filefd, -1, "open file");
//    
//    // filesize
//    off_t filesize;
//    recv(netfd, &train._length, sizeof(int), MSG_WAITALL);
//    recv(netfd, train._data, train._length, MSG_WAITALL);
//    memcpy(&filesize, train._data, train._length);
//    printf("filesize = %ld\n", filesize);
//     
//    // file
//    ftruncate(filefd, filesize);
//    char* p = (char*)mmap(NULL, filesize, PROT_READ | PROT_WRITE, MAP_SHARED, filefd, 0);
//    ERROR_CHECK(p, NULL, "mmap");
//    recv(netfd, p, filesize, MSG_WAITALL);
//    munmap(p, filesize);
//
//    close(filefd);
//
//    return 0;
//}

int epollAdd(int epfd,int fd)
{
    struct epoll_event ev;
    ev.data.fd = fd;
    ev.events = EPOLLIN;
    int epoll_ctl_res = epoll_ctl(epfd,EPOLL_CTL_ADD,fd,&ev);
    ERROR_CHECK(epoll_ctl_res,-1,"epoll add error");
    return 0;
}

int epollDel(int epfd,int fd)
{
    int epoll_ctl_res = epoll_ctl(epfd,EPOLL_CTL_DEL,fd,NULL);
    ERROR_CHECK(epoll_ctl_res,-1,"epoll delete error");
    return 0;
}


int main(int argc,char* args[])
{
    // 192.168.238.128 12345
    ARGS_CHECK(argc, 3);
    int netfd = socket(AF_INET, SOCK_STREAM, 0);
    ERROR_CHECK(netfd, -1, "socket");
    printf("socket success\n");

    struct sockaddr_in server;// 绑定服务器
    server.sin_family = AF_INET;
    server.sin_port = htons(atoi(args[2]));
    printf("client.sin_port = %d\n", server.sin_port);
    int res = inet_aton(args[1], &server.sin_addr);
    //addr.sin_addr.s_addr = inet_addr(argv[1]);
    ERROR_CHECK(res, 0, "inet_aton");
    printf("client.sin_addr  = %d\n", server.sin_addr.s_addr);
    printf("inet_aton success\n");

    res = connect(netfd, (struct sockaddr*)&server, sizeof(server));
    ERROR_CHECK(res, -1, "connect");
    printf("connect success\n");

    int epfd = epoll_create(1);
    ERROR_CHECK(epfd, -1, "epoll_create");

    epollAdd(epfd, STDIN_FILENO);
    epollAdd(epfd, netfd);
    struct epoll_event readyset[16];
    char buf[4096] = { 0 };

    while(1){
        bzero(buf, sizeof(buf));
        int readynum = epoll_wait(epfd, readyset, 16, -1);
        for(int i = 0;i < readynum; ++i){
            int cfd = readyset[i].data.fd;
            if(cfd == STDIN_FILENO){
                int len = read(STDIN_FILENO, buf, sizeof(buf));
                // deal \n
                if (len > 0) {
                    buf[len] = '\0';  
                    if (buf[len - 1] == '\n') {
                        buf[len - 1] = '\0';
                        len--;
                    }
                    send(netfd, buf, len, MSG_NOSIGNAL);
                }
            }else if(cfd == netfd){
                write(netfd, buf, sizeof(buf));
                printf("%s\n", buf);
            }
        }
    }

    recvFile(netfd);

    close(netfd);

    return 0;
}

